<?php
class IndexAction extends Action{
	
function _initialize() {
		if (!is_numeric($_GET['uid']) && is_string($_GET['uid'])) {
			$domainuser = D('User')->getUserByIdentifier(h($_GET['uid']), 'domain');
			//print_r($domainuser);
	   		if ($domainuser) {
	   			$this->uid = $domainuser['uid'];
	   			$this->assign('uid',$this->uid);
	   		}else {
   	   			$this->error('网摊不存在');
	   		}
		}

		if ('detail' != ACTION_NAME) {
			$user_info = D('User')->getUserByIdentifier($this->uid);
	   	   	if ($user_info) {
				$this->assign('userinfo',$user_info);
	   	   	} else {
   	   			$this->error('网摊不存在');
	   		}
	
	       	$this->__getSpaceBasic( $this->uid );
	       	//$this->setTitle($shopfile['shopname']);
		}
	}

	private function __getSpaceBasic($uid) {
		global $shopfile;
		
        $shopfile = M('user_basicfile')->where('uid='.$uid)->find();
        if(!$shopfile){
		   $this->assign('jumpUrl',$refer_url);
		   $this->error('该用户尚未开通网摊');
		}
		$this->assign('shopfile', $shopfile);
	}
	public function index() {
		global $shopfile;
		$map="uid='{$this->uid}'";
		if($_POST['k']){
		$search_key = $this->__getSearchKey ();
		$map.="AND title like '%{$search_key}%'";
		}
		$goods = M ( 'goods' );
		$count = $goods->where($map)->count();    //计算总数
		$p = new Page ( $count, 12 );
		$goodslist=$goods->where($map)->limit($p->firstRow.','.$p->listRows)->order('utime DESC')->findAll ();
	    $page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('goodslist',$goodslist);
		
		if($shopfile['shopname']){
		$this->setTitle($shopfile['shopname']);	
		$this->assign('shopname',$shopfile['shopname']);
		}else{
        $this->assign('shopname',$shopfile['uname'].'的网摊');
		$this->setTitle($shopfile['uname'].'的网摊');
	    }
		$this->display();
	}
	public function goods(){
	    global $shopfile;
		$id = empty($_GET['gid'])? 0: intval($_GET['gid']);
		$goods = M ( 'goods' );
		$goodslist=$goods->where('id='.$id)->find ();
		
	    if(!$goodslist){
	    	
	    	$this->error('抱歉，你所查看的内容不存在，或者已经删除!');
			exit;
	    }
	    
	    $goods->setInc('gscan','id='.$id);
		$this->assign('goods',$goodslist);
		
		$leavew = M('shop_leaveword');
		$count = $leavew->where("goods_id={$id} AND type='goods'")->count(); 
		$p = new Page ( $count, 10 );
		$lwlist=$leavew->where("goods_id={$id} AND type='goods'")->limit($p->firstRow.','.$p->listRows)->order('ctime DESC')->findAll();
		$page = $p->show ();
		$this->assign ( "page", $page );
		//print_r($lwlist);
		$this->assign('lwlist',$lwlist);
		if($shopfile['shopname']){  //页面标题
		    $this->assign('shopname',$shopfile['shopname']);
			$this->setTitle($goodslist['title'].' - '.$shopfile['shopname']);
		}else{
			
		    $this->assign('shopname',$shopfile['uname'].'的网摊');
			$this->setTitle($goodslist['title'].' - '.$shopfile['uname'].'的网摊');
		}
		
		$this->display();
	    
		
	}
	public function add_Lw_gd(){   //商品留言
	    $id = empty($_POST['goods_id'])? 0 :intval($_POST['goods_id']);
		if(!$id){
		  echo 0;
		  return;
		}
	    echo ($aa=$this->_leaveword($id,'goods',$_POST['message']))? $aa : 0;
	  
	}
	public function add_Lw_qg(){   //求购留言
	    $id = empty($_POST['goods_id'])? 0 :intval($_POST['goods_id']);
		if(!$id){
		  echo 0;
		  return;
		}
	    echo ($aa=$this->_leaveword($id,'qiugou',$_POST['message']))? $aa : 0;
	  
	}
    public function qgoods(){
	    global $shopfile;
		$id = $_GET['gid'];
		$goods = M ( 'qiugou' );
		$goodslist=$goods->where('id='.$id)->find ();
		
	    if(!$goodslist){
			$this->error('抱歉，你所查看的内容不存在，或者已经删除！');
			exit;
		}
	    	
	     $goods->setInc('gscan','id='.$id);
		 $this->assign('goods',$goodslist);
		
		 $leavew = M('shop_leaveword');
		 $count = $leavew->where("goods_id={$id} AND type='qiugou'")->count(); 
		 $p = new Page ( $count, 10 );
		 $lwlist=$leavew->where("goods_id={$id} AND type='qiugou'")->limit($p->firstRow.','.$p->listRows)->order('ctime DESC')->findAll();
		 $page = $p->show ();
		 $this->assign ( "page", $page );
		 $this->assign('lwlist',$lwlist);
		 
		if($shopfile['shopname']){  //页面标题
		    $this->assign('shopname',$shopfile['shopname']);
			$this->setTitle($goodslist['title'].' - '.$shopfile['shopname']);
		}else{
			
		    $this->assign('shopname',$shopfile['uname'].'的网摊');
			$this->setTitle($goodslist['title'].' - '.$shopfile['uname'].'的网摊');
		}
		
		$this->display();
	   
	    
	}
	public function allq(){
		global $shopfile;
	    $goods = M ( 'qiugou' );
	    $count=$goods->where('uid='.$this->uid)->count();
	    $p = new Page ( $count, 12 );
		$goodslist=$goods->where('uid='.$this->uid)->limit($p->firstRow.','.$p->listRows)->order('utime DESC')->findAll ();
	    $page = $p->show ();
	  	$this->assign ( "page", $page );
		$this->assign('goodslist',$goodslist);
	    if($shopfile['shopname']){
		$this->setTitle($shopfile['shopname']);	
		$this->assign('shopname',$shopfile['shopname']);
		}else{
        $this->assign('shopname',$shopfile['uname'].'的网摊');
		$this->setTitle($shopfile['uname'].'的网摊');
	    }
			
	    $this->display('allqgoods');	
	}
	public function Del_lw(){  //删除留言 公用
	 $id = empty($_POST['lw_id'])? 0 :intval($_POST['lw_id']);
	 
	 if(!$id){
	    echo 0 ;
		return ;
	 }
	 echo ($res = M('shop_leaveword')->where('id='.$id)->delete())? 1 : 0;
	 
	}
	//给商品留言
	 private function _leaveword($goods_id,$type='',$message){
	 
		//留言数据
		
		   $gdata['goods_id']     =   $goods_id;
		   $gdata['type']         =   $type;
		   $gdata['shop_id']      =   $this->uid;
		   $gdata['from_uid']     =   $this->mid;
		   $gdata['from_uname']   =   $this->user['uname'];
		   $gdata['message']      =   t($message);
		   $gdata['ctime']        =   time();
		   $rel = M('shop_leaveword')->add($gdata);
		//系统通知数据
		if($type=='qiugou'){
		   $data['title']      ='您发布的求购信息有新的留言';
		   $data['body']       ="<a href='index.php?app=shop&mod=index&act=qgoods&uid=$this->uid&gid=$goods_id'>".'点击这里查看详细信息'."</a>";
		}else{
		   $data['title']      ='您发布的商品有新的留言';
		   $data['body']       ="<a href='index.php?app=shop&mod=index&act=goods&uid=$this->uid&gid=$goods_id'>".'点击这里查看详细信息'."</a>";
		}
		   service('Notify')->send($gdata['shop_id'], 'shop_goodslw', $data, $this->mid);
		//$this->__doNotify($data['shop_id'], 'shop_goodslw', $ntdata, $this->mid);
		if($rel){
			return $rel;
		}else{
			
			return false;
		}
	}
    private function __getSearchKey() {
		$key = '';
		// 为使搜索条件在分页时也有效，将搜索条件记录到SESSION中
		if (isset ( $_REQUEST ['k'] ) && ! empty ( $_REQUEST ['k'] )) {
			if ($_GET ['k']) {
				$key = html_entity_decode ( urldecode ( $_GET ['k'] ), ENT_QUOTES );
			} elseif ($_POST ['k']) {
				$key = $_POST ['k'];
			}
			// 关键字不能超过30个字符
			if (mb_strlen ( $key, 'UTF8' ) > 30)
				$key = mb_substr ( $key, 0, 30, 'UTF8' );
			$_SESSION ['home_user_search_key'] = serialize ( $key );
		
		} else if (is_numeric ( $_GET [C ( 'VAR_PAGE' )] )) {
			$key = unserialize ( $_SESSION ['home_user_search_key'] );
		
		} else {
			//unset($_SESSION['home_user_search_key']);
		}
		
		return trim ( $key );
	}
}	
?>